//=============================================================================
//Version : V1.1.1
//data : 2018-01-17
//=============================================================================
//ALL IO PINS' Status is save to a GLOBAL RAM address(g_ucP0Temp,g_ucP1Temp,g_ucP2Temp,g_ucP3Temp)
//ex: if u want change P0.1 status,please do not use "=" to write P0 directly
//u should use "and" or "or" to change the g_ucP0Temp,and then write g_ucP0Temp to P0
//=============================================================================
//=============================================================================
//Name		:main.c
//Purpose	:main loop
//=============================================================================
#define		MAIN_C
#include    "includeAll.h"



void main(void)
{
    CFGWL = (CFGWL & 0x3F) | 0x80;	//WDT enable in Fast/Slow mode,disable in Idle/Stop mode
    CLRWDT = 1;					//clear watchdog
    ChangeToFastClk();
    InitRegister ();
    InitIoport ();
  Buzz(50);
	F_displayAll();
	startTime=0;
	 setTemp=75;
    while(1)
    {
			if((bPowerOnOff==0)&&(startTime>=2))
			{
					F_setLed1;
				F_setLed2;
					F_clrPowerL;
				
			}
		if(bError)
		{
			F_clrPowerL;
		}
		if(startTime==1)
		{
			PowerOff();
			
			startTime=2;
		}
			  CLRWDT = 1;					//clear watchdog
			  
		
		PowerSet();
		
	
				Error();
	
		timerControl();
		//
    }

}
void PowerSet()
{
    if((bPowerOnOff)&&(bError==0))
    {


        if(bSaveTemp==0)
        {

            if(curTemp<setTemp)
            {

				 F_setPowerL;
				 F_clrLed1;
			     F_setLed2;
            }
        }

        if(curTemp<=(setTemp-3))
        {
            bSaveTemp=0;

        }
        if(curTemp>=setTemp)
        {
            bSaveTemp=1;
            F_clrPowerL;
			F_setLed1;
			F_clrLed2;



        }
    
    }
}
void timerControl()
{
    if (timer40ms >= D_40ms)
    {
        timer40ms = 0;
		getTemp();
		if((bError==0)&&(startTime>=2))
		{
			if(flashSetTemptime>0)
			{
				if(flashSetTemptime>=10)
				{
					DisplayNumber(setTemp);
				}
				else
				{
					if(bSecFlash500ms)
					{
						DisplayNumber(setTemp);
					}
					else
					{
						F_displayPowerOff()  ;
					}
				}
				lastFlashSetTemptime=flashSetTemptime;
				if((lastFlashSetTemptime==10)&&(flashSetTemptime==9))
				{
					bSecFlash500ms=0;
					 timer500ms = 0;
				}
			}
			else if(bPowerOnOff)
			{
				DisplayNumber(curTemp);
			}
			else if(bPowerOnOff==0)
			{
				F_displayPowerOff()  ;
			}
			
		}
		if(bError2)
		{
			F_displayGaoWen()  ;
		}
		
		if((bError1)||(bError3))
		{
			F_displayAdShort();
		}
		
		
    }
	 if (timer300ms >= D_300ms)
	 {
		 timer300ms=0;
		if((bError)&&(startTime>=2))
		{
			
			
		
			errorNum++;
			if(cycleNum<5)
			{
				if(errorNum<=5)
			{
				if(errorNum<=3)
				{
					Buzz(20);
				}
			}
			else
			{
				errorNum=0;	
				cycleNum++;
			}
			}
			
		}
	 }
    if (timer500ms >= D_500ms)
    {
        timer500ms = 0;
		  bSecFlash500ms=~bSecFlash500ms;
		  if(flashSetTemptime>0)
		{
			flashSetTemptime--;
			if(flashSetTemptime==0)
			{
				bSetTemp=0;
			}
		}
    }
    if (timer1s >= D_1s)
    {
        timer1s = 0;
		bUpdateCurTemp=1;
		if(startTime==0)
		{
			startTime++;
			
		}
        timer1m++;
        if (timer1m >= D_1m)
        {
            timer1m = 0;
        }
    }
}
void Error()
{
	
		  if(bAdShort==1)
    {
        bError=1;
        bError3=1;
    }
    if(bAdOff==1)
    {
        bError=1;
        bError1=1;
    }
    if(bHigTemp==1)
    {
        bError=1;
        bError2=1;
    }
	
  
    if(bError)
    {

    F_setLed2;
    F_setLed1;
     
   
    }
	if((bError1==0)&&(bError2==0)&&(bError3==0))
	{
				bError=0;
	}
	if((lastBError==1)&&(bError==0))
	{
		PowerOff();
	}
        lastBError=bError;

}
void PowerOff()
{
	bPowerOnOff=0;
	flashSetTemptime=0;
	
	bSetTemp=0;
	F_displayPowerOff()  ;
	F_clrPowerL;
	F_setLed1;
	F_setLed2;
	cycleNum=0;
	errorNum=0;
		F_clrPowerL;
		F_setLed1;F_setLed2;
}